//
//  CZEmoticonToolBar.swift
//  53.表情键盘新工程
//
//  Created by gzxzmac on 16/3/31.
//  Copyright © 2016年 gzxzmac. All rights reserved.
//

import UIKit

// 1.定义协议
protocol CZEmoticonToolBarDelegate: NSObjectProtocol {
    // 2.定义方法
    // toolBar按钮点击了
    func emoticonToolBar(tooBar: CZEmoticonToolBar, didSelectType type: CZEmoticonToolBarButtonType)
}

/// 按钮tag对应的枚举
enum CZEmoticonToolBarButtonType: Int {
    case Recent = 0     // 最近
    case Default = 1    // 默认
    case Emoji = 2      // emoji
    case Lxh = 3        // 浪小花
}

class CZEmoticonToolBar: UIView {
    
    // MARK: - 属性
    /// 3.定义代理属性
    weak var delegate: CZEmoticonToolBarDelegate?
    
    /// 上一次选中的按钮
    private var selectedButton: UIButton?
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    init(packages: [CZEmoticonPackageModel]) {
        super.init(frame: CGRectZero)
        
        for (index, package) in packages.enumerate() {
            let packageName = package.group_name_cn
            
            let button = UIButton(type: UIButtonType.Custom)
            button.setTitle(packageName, forState: UIControlState.Normal)
            button.setBackgroundImage(UIImage(named: "compose_emotion_table_mid_normal"), forState: UIControlState.Normal)
            button.setBackgroundImage(UIImage(named: "compose_emotion_table_mid_selected"), forState: UIControlState.Selected)
            button.setBackgroundImage(UIImage(named: "compose_emotion_table_mid_selected"), forState: UIControlState.Disabled)
            button.tag = index
            
            button.addTarget(self, action: Selector("didClickButton:"), forControlEvents: UIControlEvents.TouchUpInside)
            
            addSubview(button)
        }
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let width = frame.width / CGFloat(subviews.count)
        for (index, view) in subviews.enumerate() {
            view.frame = CGRectMake(CGFloat(index) * width, 0, width, frame.height)
        }
    }
    
    @objc private func didClickButton(button: UIButton) {
        switchSelectedButton(button)
        
        // 4.调用代理,告诉keyboard选中了哪个按钮
        let type = CZEmoticonToolBarButtonType(rawValue: button.tag)!
        delegate?.emoticonToolBar(self, didSelectType: type)
    }
    
    override func willMoveToSuperview(newSuperview: UIView?) {
        super.willMoveToSuperview(newSuperview)
        
        // 默认选中第0组
        switchSelectedButtonWithSection(0)
    }
    
    // MARK: - 公开方法
    /**
    选中某个的按钮
    
    - parameter section: collectionView所对应的组
    */
    func switchSelectedButtonWithSection(section: Int) {
        // 找到对应的按钮
        let button = self.subviews[section] as! UIButton
        
        switchSelectedButton(button)
    }
    
    /**
     切换选中按钮
     
     - parameter button: 要选中按钮
     */
    private func switchSelectedButton(button: UIButton) {
//        print("选中了:\(button.tag)")
        // 当上次选中的和当前选中的是同一个按钮,不在选中
//        if button == selectedButton {
//            return
//        }
        
        // 1.取消上一次选中的按钮
        selectedButton?.enabled = true
        
        // 2.将当前按钮选中
        button.enabled = false
        
        // 3.将当前按钮赋值给上次选中的按钮
        selectedButton = button
    }
}
